import { ref } from 'vue'
import { message } from 'ant-design-vue'
import { login } from '@/api/common'

// 用户认证管理
export function useAuth() {
  const uid = ref('')

  const loginUser = async (username = 'admin', password = 'ids1@3') => {
    try {
      // 对密码进行base64编码
      const encodedPassword = btoa(password)
      const response = await login(username, encodedPassword)
      uid.value = response.data.uid
      return response
    } catch (error) {
      message.error(error.message || '登录失败')
      throw error
    }
  }

  return {
    uid,
    loginUser
  }
}
